package net.jlibrary.invoice.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.invoice.action.vo.GoodsActionVo;
import net.jlibrary.invoice.domain.SlKind;
import net.jlibrary.invoice.service.IGoodsManager;

@RequestMapping("/goods")
@RestController
public class GoodsAdminAction {
	@Autowired
	private IGoodsManager goodsManager;

	@RequestMapping(value = "/goodsAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/electronic/goodsAdminView");
	}

	// 获得商品列表
	@RequestMapping(value = "/goodsAdminJsonList", method = { RequestMethod.POST })
	public Pagination lists(GoodsActionVo goods, PaginationParam param) {
		Pagination pagination = goodsManager.listsPage(goods, param);
		return pagination;
	}

	// 商品详情
	@RequestMapping(value = "/goodsAdminJsonDetail", method = { RequestMethod.GET })
	public GoodsActionVo detail(GoodsActionVo goods) {
		goods = goodsManager.get(goods);
		return goods;
	}

	// 初始化商品
	@RequestMapping(value = "/goodsAdminJsonAddFormInit", method = { RequestMethod.GET })
	public GoodsActionVo addFormInit() {
		GoodsActionVo goods = new GoodsActionVo();
		return goods;
	}

	// 保存商品信息
	@RequestMapping(value = "/goodsAdminJsonSave", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") GoodsActionVo goods, @Token String token, BindingResult result) {
		goodsManager.save(goods);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 修改商品信息
	@RequestMapping(value = "/goodsAdminJsonUpdate", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") GoodsActionVo goods, @Token String token, BindingResult result) {
		goodsManager.update(goods);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 删除商品信息
	@RequestMapping(value = "/goodsAdminJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		GoodsActionVo goods = new GoodsActionVo();
		goods.setId(id);
		goodsManager.delete(goods);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	// 恢复商品信息
	@RequestMapping(value = "/goodsAdminJsonRenew", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		GoodsActionVo goods = new GoodsActionVo();
		goods.setId(id);
		goodsManager.renew(goods);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/goodsSldataJson", method = { RequestMethod.GET })
	public List<ModelMap> goodsSldataJson() {
		List<SlKind> slKinds = goodsManager.slKinds();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (SlKind type : slKinds) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, type.getId());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, type.getName());
			maps.add(map);
		}
		return maps;
	}

}
